package amit.util;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;

import amit.ad.AdDetails;
import amit.ui.window.AmitMainFrame;

/**
 * run on the site each time with specific query
 * 
 * @author ido
 * 
 * query
 * 
 * http://homeless.co.il/rent/default.asp?Search=1&OrderBy=&AscDesc=&Page=1&
 * iNumber1=17
 * 
 * <option class="boldRed" value="186,17,2,1,150,185,3,12,11,168,4,128">???? ?"?
 * ??????</option> <option value="1, 2, 17, 150, 185">?? ???? ????</option>
 * <option value="17">??-???? ????</option> <option value="2">??-???? ????</option>
 * 
 * <option value="1">??-???? ???? ????</option> <option value="150">?"? ??????
 * ??????</option> <option value="185">???? ?"? ?"?</option> <option
 * value="3">?"? - ???????</option> <option value="12">?"? ???????</option>
 * <option value="11">???? ???? ???????</option> <option value="168">????"?</option>
 * <option value="4">?? ?? - ?????</option> <option value="128">?????? - ?.????</option>
 * 
 * <option class="boldRed" value="174,175,177,176,41,158,127">???? ?????</option>
 * <option value="174">??? ?????, ??????</option> <option
 * value="175">?????,?.???,?.?????</option> <option value="177">????? ????</option>
 * <option value="176">????? ???????</option> <option value="41">???? ???????</option>
 * <option value="158">?????? ???????</option> <option value="127">?????? -
 * ?????</option> <option class="boldRed" value="7,13,14,130">???? ???????</option>
 * 
 * <option value="7">??????? ???????</option> <option value="13">???????,????
 * ???????</option> <option value="14">????? ???????</option> <option
 * value="130">?????? - ???????</option> <option class="boldRed"
 * value="8,171,15,16,43,126">???? ?????</option> <option value="8">????
 * ???????</option> <option value="171">??????, ???, ?????</option> <option
 * value="15">????? ???????</option> <option value="16">????? ???????</option>
 * 
 * <option value="43">????? ??????</option> <option value="126">?????? - ????</option>
 * <option class="boldRed" value="173,6,181,182">???? ?????</option> <option
 * value="173">????-???</option> <option value="6">?? ????? ??????</option>
 * <option value="181">???? ???????</option> <option value="182">?????? - ?????</option>
 * <option class="boldRed" value="42,9,170,129">???? ?????</option> <option
 * value="42">?????, ?????? </option>
 * 
 * <option value="9">??? ??? ??????</option> <option value="170">???? </option>
 * <option value="129">?????? - ????</option> <option class="boldRed"
 * value="10,178,147">????</option> <option value="10">?????? - ????</option>
 * <option value="178">?????? ?????</option> <option value="147">?? ????</option>
 * 
 * &iNumber3=11&iNumber4=&iNumber4_1=& fLong1_1=1000& ???? SearchFor=&x=25&y=16
 * 
 * =========== to next page ========================== nowrap><A
 * class="TableTitle" title="????? ???"
 * HREF="?page=2&a=a&Search=1&iNumber4=&iNumber4_1=&OrderBy=&AscDesc=&fLong1_1=1000&iNumber1=17&iNumber3=11&SearchFor=&x=25&y=16"><IMG
 * 
 * or bgcolor="#FFFFFB">???? <font color="#FF3000">1</font> ???? <font
 * color="#FF3000">8</font>&nbsp;
 * 
 * ===========
 * http://homeless.co.il/sale/default.asp?Search=1&OrderBy=&AscDesc=&
 * Page=1&iNumber1=1%2C+2%2C+17%2C+150%2C+185&iNumber3=11&iNumber4=2&iNumber4_1
 * =4&fLong1=50000&fLong1_1=160000&SearchFor=&x=15&y=15
 * 
 * 
 * &iNumber3=11 --> DIRA
 * &iNumber3=7  --> 'BIAT PRATY'
 */
public class Runner {

	public static final String BASE_HOST_RENT = "http://homeless.co.il/rent/default.asp";
	public static final String BASE_HOST_SALE = "http://homeless.co.il/sale/default.asp";
	public static final String AD_DETAILS_RENT_LINK = "http://www.homeless.co.il/Upload/rent/";
	public static final String AD_DETAILS_SALE_LINK = "http://www.homeless.co.il/Upload/sale/";
	public String TMP_FILE_NAME = "c:/temp/homeless_";
	
	private HttpClient client;
	private String parentUrl;
	private boolean isScanParent;
	private boolean isRentMode;
	private ArrayList<String> pageUrls;
	private String outputFileName;

	/**
	 * 
	 * @param url
	 */
	Runner(String url, String outputFileName) {
		this.parentUrl = url;
		this.outputFileName = FileUtil.DATA_DIR + outputFileName;
		client = new HttpClient(new MultiThreadedHttpConnectionManager());
		client.getHttpConnectionManager().getParams().setConnectionTimeout(
				30000);

	}

	private void loadParentPage() {
		GetMethod get = new GetMethod(parentUrl);
		get.setFollowRedirects(true);
		System.out.println("## going to fetch the parent Url:" + parentUrl);
		try {
			int iGetResultCode = client.executeMethod(get);
			final String strGetResponseBody = get.getResponseBodyAsString();
			if (strGetResponseBody != null) {
				// PARSE the HTML
				/*
				 * int indx1 = parentUrl.indexOf("&Page="); StringBuffer
				 * outFileName = new StringBuffer(TMP_FILE_NAME); if
				 * (isRentMode) { outFileName.append("rent"); } else {
				 * outFileName.append("sale"); }
				 * outFileName.append(parentUrl.substring(indx1+6, indx1+8));
				 * outFileName.append(".htm");
				 * FileUtil.sharedInstance.writeToFile( outFileName.toString(),
				 * strGetResponseBody);
				 */
				parseThePage(strGetResponseBody);
			}
		} catch (Exception ex) {
			System.out.println("####ERR error in Parent page: "+parentUrl);
			ex.printStackTrace();
		} finally {
			get.releaseConnection();
		}

	}

	/**
	 * 
	 * @param url
	 * @param fileID
	 */
	private void loadPage(final String url, final String fileID) throws Exception{
		// create a new thread to load the URL from
		Thread loadPage = new Thread() {
			public void run() {
				FetchAllAds.number_of_running_scan_threads++;
				GetMethod get = new GetMethod(url);
				get.setFollowRedirects(true);
				System.out.println(" thread no: " +FetchAllAds.number_of_running_scan_threads +
						"## going to fetch the page:" + url);
				try {
					int iGetResultCode = client.executeMethod(get);
					final String strGetResponseBody = get
							.getResponseBodyAsString();
					if (strGetResponseBody != null) {
						parseThePage(strGetResponseBody);
					}
				} catch (Exception ex) {
					System.out.println("####ERR error in page: "+url);
					ex.printStackTrace();
				} finally {
					get.releaseConnection();
					FetchAllAds.number_of_running_scan_threads--;
				}
			}
		};
		loadPage.start();
	}

	private void parseThePage(String pageBody) throws Exception {
		// System.out.println("Start the parsing...");
		String ANCOR1 = "ShowHideMessageText(";
		int index1 = 0, index2 = 0, index3 = 0, tdIndex=0;
		do {
			String adID="";
			String[] val = new String[6];
			for (int i = 0; i < 6; i++) {
				index1 = pageBody.indexOf(ANCOR1, index3);
				if (i ==0) {
					// get the ad number ID
					index2 = pageBody.indexOf("'", index1 + 2);
					index3 = pageBody.indexOf("'", index2 + 2);
					adID = pageBody.substring(index2+1, index3);
				}
				tdIndex = pageBody.indexOf("><TD", index1+10);
				if (tdIndex > index1 + 30) {
					// we don't have this extra TD
					tdIndex = index1 + 5;
				}
				index2 = pageBody.indexOf(">", tdIndex + 2);
				index3 = pageBody.indexOf("<", index2 + 2);
				String tmpStr = pageBody.substring(index2 + 1, index3).trim();
				val[i] = tmpStr.replace("$", "");
				val[i] = val[i].replace(",", "");
				val[i] = val[i].replace("\n", "");
			}
			// String type, float rooms, int price, String address,String
			// city, String comments)
				if (val[1].length() > 0 && val[2].length() > 1) {
					int price = Integer.parseInt(val[2]);
					if (!isRentMode() && price < 1000) {
						price = price * 1000;
					}
					AdDetails ad = new AdDetails(adID, val[0], Float
							.parseFloat(val[1]), price, val[3], val[4], val[5]);
					//@@ System.out.println("Ad: " + ad.toString());
					FileUtil.sharedInstance.writeToCSVFile(outputFileName, ad
							.getAsCVSString());
				}
			// skip 3 anchors
			index3 = pageBody.indexOf(ANCOR1, index3 + 3);
			index3 = pageBody.indexOf(ANCOR1, index3 + 3);
			index3 = pageBody.indexOf(ANCOR1, index3 + 3) + 10;
		} while (pageBody.indexOf(ANCOR1, index3) > index2);

		if (isScanParent) {
			// lets find the other pages for this query
			String pageEnd = "Paging1";
			int endIndex = pageBody.indexOf(pageEnd);
			int TDindex = pageBody.indexOf("TD>", endIndex + 10);
			do {
				index1 = pageBody.indexOf("?page=", endIndex + 6);
				index2 = pageBody.indexOf(">", index1 + 10);
				if (index1 < TDindex) {
					if (isRentMode()) {
						pageUrls.add(BASE_HOST_RENT
								+ pageBody.substring(index1, index2));
					} else {
						pageUrls.add(BASE_HOST_SALE
								+ pageBody.substring(index1, index2));
					}
				}
				endIndex = index2 + 2;
			} while (index1 < TDindex);
			// in case we have more then 20 pages
			int lastPageIndex = pageBody.indexOf("<TD align=left class=\"BoldBrown\" bgcolor=\"#FFFFFB\">",
					TDindex + 10);
			int tmpIndex = pageBody.indexOf("FF3000", lastPageIndex +20);
			tmpIndex = pageBody.indexOf("FF3000", tmpIndex + 10);
			tmpIndex = pageBody.indexOf(">", tmpIndex+2);
			int tmpIndex2 = pageBody.indexOf("<", tmpIndex+2);
			int maxPage = Integer.parseInt(pageBody.substring(tmpIndex+1, tmpIndex2));
			for (int i = 21; i < maxPage; i++) {
				String pageUrlStr = pageUrls.get(1);
				tmpIndex = pageUrlStr.indexOf("page=");
				StringBuffer sb = new StringBuffer(pageUrlStr.substring(0,tmpIndex+5));
				sb.append(i);
				sb.append(pageUrlStr.substring(tmpIndex+6));
				pageUrls.add(sb.toString());
			}
		}
	}

	public void runOnQuery() throws Exception {
		pageUrls = new ArrayList<String>();
		isScanParent = true;
		// unit test
		// parseThePage(FileUtil.sharedInstance.readTextFile(TMP_FILE_NAME));
		loadParentPage();
		isScanParent = false;
		int i = 0;
		for (Iterator iter = pageUrls.iterator(); iter.hasNext();) {
			String tmpUrl = (String) iter.next();
			loadPage(tmpUrl, "" + i++);
		}
	}

	public boolean isScanParent() {
		return isScanParent;
	}

	public void setScanParent(boolean isScanParent) {
		this.isScanParent = isScanParent;
	}

	public String getParentUrl() {
		return parentUrl;
	}

	public void setParentUrl(String parentUrl) {
		this.parentUrl = parentUrl;
	}

	public HttpClient getClient() {
		return client;
	}

	public boolean isRentMode() {
		return isRentMode;
	}

	public void setRentMode(boolean isRentMode) {
		this.isRentMode = isRentMode;
	}

	public ArrayList<String> getPageUrls() {
		return pageUrls;
	}

	/**
	 * unit test
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		System.out.println("@UNIT TEST@ going to parse ALL tel aviv"); //$NON-NLS-1$
		Runner runnerTA = new Runner(Messages.getString("FetchAllAds.sale.ta.url"), //$NON-NLS-1$
			Messages.getString("FetchAllAds.sale.ta.fielname")); //$NON-NLS-1$
		runnerTA.setRentMode(false);
		runnerTA.runOnQuery();

		System.out.println("UNIT TEST: going to parse South country area"); 
		Runner runner6 = new Runner(Messages.getString("FetchAllAds.sale.south.url"), //$NON-NLS-1$
			Messages.getString("FetchAllAds.sale.south.filename")); //$NON-NLS-1$
		runner6.setRentMode(false);
		runner6.runOnQuery();

	}
}
